$(function () {
    $("#jqGrid").Grid({
        url: '../materialbase/list?type=1',
        colModel: [
            {label: '领料单号', name: 'materCode',align: 'center', index: 'MATER_CODE', width: 120},
            // {label: '领料类别',align: 'center', width: 80,
            //     formatter: function (value, col, row) {
            //         return sysEnums.MATER_TYPE[row.materialType];
            //     }
            // },
            {label: '单据状态',align: 'center', width: 80,
                formatter: function (value, col, row) {
                    return sysEnums.MATER_PROGRESS[row.progress];
                }
            },
            {label: '领用部门', name: 'deptId',align: 'center', index: 'DEPT_ID', width: 120},
            {label: '领料人', name: 'picker', align: 'center', index: 'PICKER', width: 80},
            {label: '领料时间', name: 'pickDate',align: 'center', index: 'PICK_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd  hh:mm');
                }
            },
            {label: '用途', name: 'purpose',align: 'center', index: 'PURPOSE', width: 120},
            {label: '仓管员', name: 'storeker', align: 'center',index: 'STOREKER', width: 80},
            {label: '备注信息', name: 'remark',align: 'center', index: 'REMARK', width: 120},
            {
                label: '审批详情', name: 'devCode', index: 'DEV_CODE', width: 80,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' id='recordsText' onclick='vm.shviewOption(\"" + escape(JSON.stringify(row)) + "\")'>&nbsp;详情</button>";
                }
            },
            {
                label: '领料单清单', width: 80, align: 'center', sortable: false,
                formatter: function (value, col, row) {
                    // flag为0时表示可以编辑，否则不可以
                    let flag = (row.progress == 1 || row.progress == 4)?0 : 1;
                    return "<button title='清单详情' class='btn btn-outline btn-info' onclick='vm.showDetial(\"" + row.id + "\",\"" + flag + "\")'><i class='fa fa-file-text-o'></i></button>";
                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '领料单状态', name: 'progress', hidden: true},
            {label: '审核状态', name: 'isCheck', hidden: true}
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        materialBase: {
            remark: '',
            type: '',
            jobCode: '',
            storeker: '',
            approveDate: '',
            approver: '',
            pickDate: '',
            picker: '',
            purpose: '',
            deptId: '',
            isCheck: 0,
            isOk: 0,
            checkRemark: '',
            id: '',
        },
        checkData:{
            progress:'',
            checkRemark:''
        },
        ruleValidate: {
            purpose: [
                {required: true, message: '用途不能为空', trigger: 'blur'}
            ]
        },
        q: {
            materCode:'', // 领用单号
            progress:'', // 领料单进度
            picker: '' // 领料人
        },
        checkList:enumsToList('MATER_CHECK_FLAG'),//领料单审批状态
        okList:enumsToList('MATER_FLAG'),//领料单完成状态
        materProgress: enumsToList('MATER_PROGRESS'),//领料单状态
    },
    methods: {
        //审核详情
        shviewOption: function (res) {
            let data = JSON.parse(unescape(res))
            if (!data.approver) {
                data.approver = "-"
            }
            if(data.progress == 1 || data.progress == 2){
                iview.Message.info("当前工单尚未审核,请耐心等待!");
                return;
            }
            let dom = "<div class='details' >" +
                "<table>" +
                "<tr><td>审批人：</td><td>" + data.approver + "</td></tr>" +
                "<tr><td>审批状态：</td><td>" + sysEnums.MATER_CHECK_FLAG[data.isCheck] + "</td></tr>" +
                "<tr><td>审批时间：</td><td>" + transDate(data.approveDate, "yyyy-MM-dd") + "</td></tr>" +
                "<tr><td>审批备注：</td><td>" + (data.checkRemark?data.checkRemark:'暂无') + "</td></tr>" +
                "</table></div>"
            layer.open({
                type: 1,
                title: "审批详情",
                closeBtn: 0,
                area: ['450px'],
                offset: '10vh',
                shadeClose: true,
                skin: 'yourclass',
                content: dom
            });
        },
        commit: function(){
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            var data = getSelectedRowData("#jqGrid");
            if(data.progress != '1' && data.progress != '4'){
                iview.Message.error("只有新建或审核驳回的领料单才可以提交审核!");
                return;
            }
            // 领料单编制完毕，提交审核
            Ajax.request({
                url: "../spareMater/commitApprove/" + id,
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('提交成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.materialBase = {
                remark: '',
                type: 1,
                jobCode: '',
                storeker: '',
                materCode: '',
                approveDate: '',
                approver: '',
                pickDate: '',
                isCheck: 2,
                isOk: 0,
                checkRemark: '',
                picker: '',
                purpose: '',
                deptId: '',
                id: '',
                materialType: 1
            };
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.materialBase.id == (null || '') ? "../materialbase/save" : "../materialbase/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.materialBase),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../materialbase/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../materialbase/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.materialBase = r.materialBase;
                }
            });
        },
        //审批处理
        check: function () {
            let ids = getSelectedRow("#jqGrid");
            if (ids == null) {
                return;
            }
            var data = getSelectedRowData("#jqGrid");
            if(data.progress==1){
                iview.Message.error("新增状态的领料单无法审核！");
            }else if(data.progress==3 || data.progress==5){
                iview.Message.error("已审核通过,无需重复审核！");
            }else{
                //设置默认1审核通过
                vm.checkData.isCheck=1;
                vm.checkData.checkRemark='';
                openWindow({
                    title: "审核",
                    area: ['400px', '300px'],
                    content: jQuery("#check_ok"),
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        let obj = {"progress":vm.checkData.progress,'checkRemark':vm.checkData.checkRemark,'id':data.id};
                        Ajax.request({
                            url: "../spareMater/check",
                            params: obj,
                            type: "POST",
                            successCallback: function (r) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            }
                        })
                        layer.close(index);
                    }
                });

            }
        },
        outMater:function(){

            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            var data = getSelectedRowData("#jqGrid");

            if(data.isOk==1){
                iview.Message.error("当前已经出库完成！");
            }else{
                //出库操作
                openWindow({
                    type: 2,
                    title: '<i class="fa fa-file-text-o"></i> 选择工装清单',
                    content: '../tools/outList.html?id='+id,
                    btn: ['出库', '取消'],
                    btn1: function (index) {
                        //领料
                        //出库处理
                        Ajax.request({
                            url: "../materialbase/out/" + id,
                            type: "POST",
                            contentType: "application/json",
                            successCallback: function (r) {
                                alert('出库成功', function (index) {
                                    vm.reload();
                                });
                            }
                        });
                        layer.close(index);
                    }
                })
            }

        },
        showDetial:function(id,flag){
            openWindow({
                type: 2,
                area:['100%','100%'],
                title: '<i class="fa fa-file-text-o"></i> 选择工装清单',
                content: '../tools/materialrecord.html?materId='+id+'&flag='+flag,
                /*btn: ['出库', '取消'],
                btn1: function (index) {
                    vm.maintain_plan.dept = node[0].parentId;
                    if (node[0].deptType == 'DT_CJKSCX') {
                        // //选择上级菜单
                        vm.maintain_plan.beltline = node[0].deptId;
                        vm.maintain_plan.beltlineName = node[0].name;
                        layer.close(index);
                    }
                }*/
            })
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                materCode:'', // 领用单号
                progress:'', // 领料单进度
                picker: '' // 领料人
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});
